//
//  JFInternationalHelper.m
//  JFBase
//
//  Created by Alan Wang on 2019/3/6.
//  Copyright © 2019 Alan Wang. All rights reserved.
//

#import "JFInternationalHelper.h"
#define JF_AW_AppLanguage @"JF_AW_AppLanguage"
#define JF_AW_FontScaleCoefficient @"JF_AW_FontScaleCoefficient"


@implementation JFInternationalHelper


+(void)fontScaleCoefficient:(NSUInteger)scale{
    if(scale>=1 && scale <= 5){
        [[NSUserDefaults standardUserDefaults] setInteger:scale forKey:JF_AW_FontScaleCoefficient];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:KNotify_FontScale_Changed object:nil];
    }
}
+(NSUInteger)getFontScaleCoefficient{
    NSInteger font =  [[NSUserDefaults standardUserDefaults] integerForKey:JF_AW_FontScaleCoefficient];
    if (font == 0){
        font = 3;
    }
    return font;
}
//language  eg:en.lproj  language = en
+(void)changeLanguage:(NSString*)language{
    NSString * currentLanguage = [[NSUserDefaults standardUserDefaults] stringForKey:JF_AW_AppLanguage];
    if (![currentLanguage isEqualToString:language]) {
        if (language && language.length){
            [[NSUserDefaults standardUserDefaults] setObject:language forKey:JF_AW_AppLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [[NSNotificationCenter defaultCenter] postNotificationName:KNotify_Language_Changed object:nil];
        }
    }
}
//读取main buddle下的语言文件
+(NSString *)localizedStringWithKey:(NSString *)key{
    return [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[self currentLanguage] ofType:@"lproj"]] localizedStringForKey:key value:@"" table:nil];
}
//读取指定buddle下的语言文件
+(NSString *)localizedStringWithKey:(NSString *)key withBuddleName:(NSString *)buddleName{
    NSString *buddlePath = [[NSBundle mainBundle] pathForResource:buddleName ofType:@"bundle"];
    if(buddlePath){
        NSBundle * buddle = [NSBundle bundleWithPath:buddlePath];
        if(buddle){
            NSString * lprojPath = [buddle pathForResource:[self currentLanguage] ofType:@"lproj"];
            if (lprojPath){
                NSString * str =  [[NSBundle bundleWithPath:lprojPath] localizedStringForKey:key value:@"" table:nil];
                return str;
            }else{
                return @"";
            }
        }else{
            return @"";
        }
    
    }else{
        return @"";
    }
}
+ (NSString*)currentLanguage{
    NSString * language  = [[NSUserDefaults standardUserDefaults] objectForKey: JF_AW_AppLanguage];
    if ( language == nil){//如果没有设置过语言 就读取系统的设置
        NSString *str = nil;
        NSString *currentLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"][0];
        if ([currentLanguage hasPrefix:@"zh-Hant"]
            || [currentLanguage hasPrefix:@"zh-Hant-CN"]
            || [currentLanguage hasPrefix:@"zh-Hant-MO"]
            || [currentLanguage hasPrefix:@"zh-Hant-HK"]
            || [currentLanguage hasPrefix:@"zh-Hant-TW"]
            || [currentLanguage hasPrefix:@"zh-Hant-SG"])
        {
            str = @"zh-Hant";   //繁体
        }
        else if ([currentLanguage hasPrefix:@"zh-Hans"]
                 || [currentLanguage hasPrefix:@"zh-Hans-CN"]
                 || [currentLanguage hasPrefix:@"zh-Hans-MO"]
                 || [currentLanguage hasPrefix:@"zh-Hans-HK"]
                 || [currentLanguage hasPrefix:@"zh-Hans-TW"]
                 || [currentLanguage hasPrefix:@"zh-Hans-SG"])
        {
            str = @"zh-Hans";  //简体
        }
        else if ([currentLanguage hasPrefix:@"en"])
        {
            str = @"en";
        }
        else   //其它情况显示英文
        {
            str = @"en";
        }
        [self changeLanguage:str];
        return str;
    }else{
        return language;
    }
}

@end
